package com.hibernate.ano;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author Kevin
 * @Description: 获取注解
 * @date 2021/9/6 10:54
 */
public class AnnotationTest {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
        // 获取类上的注解
        Class<AnnotationDemo> clazz = AnnotationDemo.class;
        MyAnnotation anoOnClass = clazz.getAnnotation(MyAnnotation.class);
        System.out.println(anoOnClass.getValue());

        // 获取成员变量上的注解
        Field name = clazz.getField("name");
        MyAnnotation anoOnField = name.getAnnotation(MyAnnotation.class);
        System.out.println(anoOnField.getValue());

        // 获取 hello 方法上的注解
        Method method = clazz.getMethod("hello");
        MyAnnotation anoOnMethod = method.getAnnotation(MyAnnotation.class);
        System.out.println(anoOnMethod.getValue());

        // 获取 defaultMethod 方法上的注解
        Method defaultMethod = clazz.getMethod("defaultMethod");
        MyAnnotation anoOnDefaultMethod = defaultMethod.getAnnotation(MyAnnotation.class);
        System.out.println(anoOnDefaultMethod.getValue());
    }
}
